 package com.example.smartcity4.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity4.MainActivity;
import com.example.smartcity4.R;
import com.example.smartcity4.javaBean.GsonBean;
import com.example.smartcity4.javaBean.LoginBean;
import com.example.smartcity4.math.MathActivity;
import com.example.smartcity4.reteofi.SmartService;
import com.google.gson.Gson;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView title;
    private ImageView left;
    private ImageView more;
    private EditText item1;
    private EditText item2;
    private LinearLayout logon;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);
        initView();
    }

    private void initView() {
        title = (TextView) findViewById(R.id.title);
        left = (ImageView) findViewById(R.id.left);
        more = (ImageView) findViewById(R.id.more);
        item1 = (EditText) findViewById(R.id.item1);
        item2 = (EditText) findViewById(R.id.item2);
        logon = (LinearLayout) findViewById(R.id.logon);
        btn = (Button) findViewById(R.id.btn);
        logon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), LogOnActivity.class));
            }
        });
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String item1String = item1.getText().toString().trim();
        if (TextUtils.isEmpty(item1String)) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }

        String item2String = item2.getText().toString().trim();
        if (TextUtils.isEmpty(item2String)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something
        
        Retrofit retrofit = new Retrofit.Builder().baseUrl(MainActivity.host)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        SmartService smartService = retrofit.create(SmartService.class);
        LoginBean loginBean = new LoginBean(item1String, item2String);
        Gson gson = new Gson();
        String bean = gson.toJson(loginBean);
        final RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), bean);
        Call<GsonBean> call = smartService.post_login(requestBody);
        call.enqueue(new Callback<GsonBean>() {
            @Override
            public void onResponse(Call<GsonBean> call, Response<GsonBean> response) {
                if (Integer.valueOf(response.body().code) == 200) {
                    Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                    getSharedPreferences("SP", MODE_PRIVATE).edit().putString("token",response.body().token).apply();
                    MainActivity.token = response.body().token;
                    startActivity(new Intent(getApplicationContext(), MainActivity.class));
                    finish();

                } else {
                    Toast.makeText(getApplicationContext(), "" + response.body().msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<GsonBean> call, Throwable throwable) {
                System.out.println(throwable);
            }
        });


    }
}